Open and modify files with python


In [45]:
import sys
import fileinput
num=234e-6
n_found=0
text="* Simulación Circuito Espejo de Corriente con Ncmos, valores reales de Kp_n y Vt"+"\n"+ \
"* Universidad Nacional de Colombia 2016"+"\n"+ \
"* CMOS Analógico"+"\n"+ \
"* Grupo Jorge Garzón, Esteban Iafrancesco A"+"\n"+ \
"\n"+\
"VDD VDD 0 DC 10 AC 0"+"\n"+\
"V2 VR 0 DC 10 AC 0"+"\n"+\
"VRD RDN VR DC 0 AC 0"+"\n"+\
"RD RDN DRAIN 1000"+"\n"+\
"RP VDD GATE 2000"+"\n"+\
"M1 DRAIN GATE 0 0 nmos W=60 L=2"+"\n"+\
"M2 GATE GATE 0 0 nmos W=60 L=2"+"\n"+\
"\n"+\
"VRD2 RDN2 VR DC 0 AC 0"+"\n"+\
"RD2 RDN2 DRAIN2 1000"+"\n"+\
"RP2 VDD GATE2 2000"+"\n"+\
"M3 DRAIN2 GATE2 0 0 nmos2 W=60 L=2"+"\n"+\
"M4 GATE2 GATE2 0 0 nmos2 W=60 L=2"+"\n"+\
"\n"+\
".model nmos nmos LEVEL=1 Vto=0.8 KP=120u LAMBDA=0.01 U0=650"+"\n"+\
".model nmos2 nmos LEVEL=1 Vto=1.5 KP=130u LAMBDA=0.02 U0=600"+"\n"+\
"\n"+\
".control"+"\n"+\
"set color0 =white"+"\n"+\
"set color1=black"+"\n"+\
"op"+"\n"+\
"show all"+"\n"+\
"dc vdd 0 10 0.01"+"\n"+\
"plot i(vrd) i(vrd2)"+"\n"+\
".endc"+"\n"


for i, line in enumerate(fileinput.input('../spice-simulations/espejoNmosPythonFile.cir', inplace=1)):
    if i == 1: sys.stdout.write(text) # replace 'sit' and write
fileinput.close()

In [ ]:


In [ ]:
if line.startswith('0.000234345') == True:
        n_found += 1
        sys.stdout.write('\n')
        sys.stdout.write(line.replace(line, ".model nmos nmos LEVEL=1 Vto=0.8 KP=120u LAMBDA=0.01 U0=650"+"\n"))
sys.stdout.write('\n')  # write a blank line after the 5th line
sys.stdout.write(line.replace('dsfsdfwewef**', str(num)))  # replace 'sit' and write